package handler

import (
	"encoding/json"
	"guo-qing-hw2/cache"
	"guo-qing-hw2/model"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// 配送员添加

func AuthorAdd(c *gin.Context) {
	author_code := c.PostForm("author_code")
	author_name := c.PostForm("author_name")

	author := model.Author{
		AuthorCode: author_code,
		AuthorName: author_name,
	}

	err := model.DB.Create(&author).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单添加

func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_address := c.PostForm("order_address")
	aid, _ := strconv.Atoi(c.PostForm("aid"))

	order := model.Order{
		OrderCode:    order_code,
		OrderAddress: order_address,
		Aid:          aid,
	}

	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 配送员订单列表

func AuthorOrderList(c *gin.Context) {
	/*
		select
		orders.id as OrderId,
		orders.order_code as OrderCode,
		orders.order_address as OrderAddress,
		`authors`.author_code as AuthorCode,
		`authors`.author_name as AuthorName
		from orders
		left join `authors` on `authors`.id = orders.aid
	*/

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var authorOrderList []model.AuthorOrderList

	redis_key := "order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_address as OrderAddress",
				"`authors`.author_code as AuthorCode",
				"`authors`.author_name as AuthorName",
			).
			Joins("left join `authors` on `authors`.id = orders.aid").
			Offset(offset).Limit(size).Debug().Find(&authorOrderList)
		jsonData, _ := json.Marshal(authorOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &authorOrderList)
	}

	var total int64
	model.DB.Model(&model.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表获取成功",
		"data":  authorOrderList,
		"total": total,
	})
	return
}

// 配送员订单详情

func AuthorOrderSelect(c *gin.Context) {
	id := c.Query("id")

	var authorOrderList model.AuthorOrderList

	redis_key := "order_select_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_address as OrderAddress",
				"`authors`.author_code as AuthorCode",
				"`authors`.author_name as AuthorName",
			).
			Joins("left join `authors` on `authors`.id = orders.aid").
			Where("orders.id=?", id).
			Debug().Find(&authorOrderList)
		jsonData, _ := json.Marshal(authorOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &authorOrderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "搜索成功",
		"data": authorOrderList,
	})
	return
}
